package com.gitee.zhangchenyan.takin.auth;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import com.gitee.zhangchenyan.takin.cache.IJedisClient;
import com.gitee.zhangchenyan.takin.common.result.Result;
import com.gitee.zhangchenyan.takin.common.result.ResultUtils;
import com.gitee.zhangchenyan.takin.common.utils.HttpServletUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @Deacription 验证登录实现
 * @Author zl
 * @Date 2021/12/7 20:22
 * @Version 1.0
 **/
@Component
@Slf4j
public class AuthCheckLoginHandlerInterceptor implements AsyncHandlerInterceptor {

    @Autowired
    private IJedisClient jedisClient;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //静态资源绕过
        if (handler instanceof ResourceHttpRequestHandler) {
            return true;
        }

        //获取登录验证注解
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        AuthCheckLogin takinCheckLogin = handlerMethod.getMethodAnnotation(AuthCheckLogin.class);
        if (takinCheckLogin == null)
            return true;
        if (!takinCheckLogin.isVerify())
            return true;
        //开始验证
        String xToken = "token";
        String bToken = "Token";
        String token = request.getHeader(xToken);
        if (!StringUtils.hasLength(token)) token = request.getHeader(bToken);
        if (!StringUtils.hasLength(token)) token = HttpServletUtils.getCookie(xToken);
        if (!StringUtils.hasLength(token)) token = HttpServletUtils.getCookie(bToken);
        if (!StringUtils.hasLength(token)) token = HttpServletUtils.getSession(xToken);
        if (!StringUtils.hasLength(token)) token = HttpServletUtils.getSession(bToken);
        if (StringUtils.hasLength(token) && isLogin(token)) {
            request.setAttribute("token", token);
            return true;
        }
        Result result = ResultUtils.notLogin();
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(result));
        writer.flush();
        return false;
    }

    private boolean isLogin(String token) {
        String redisTokenKey = AuthUserServiceImpl.REDIS_TOKEN_KEY + token;
        String user = jedisClient.get(redisTokenKey);
        return StrUtil.isNotEmpty(user);
    }


}
